package weaver.conn;


import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.StatementInterceptor;
import weaver.conn.WeaverConnection;
import weaver.general.Util;

public class WeaverPreparedStatement implements PreparedStatement {
    private boolean needSplitTable = false;

    private WeaverConnection connection;

    private PreparedStatement preparedStatement;

    private String sql;

    private Map<Integer, Object> params = new HashMap<>();

    private List<Map<Integer, Object>> batchParams = new ArrayList<>();

    public WeaverPreparedStatement(WeaverConnection paramWeaverConnection, String paramString) throws SQLException {
        if (needIntercept(paramString)) {
            this.needSplitTable = true;
            this.connection = paramWeaverConnection;
            this.sql = paramString;
        } else {
            this.sql = paramString;
            this.preparedStatement = paramWeaverConnection.getConnection().prepareStatement(paramString);
        }
    }

    public WeaverPreparedStatement(WeaverConnection paramWeaverConnection, String paramString, boolean paramBoolean) throws SQLException {
        this.needSplitTable = paramBoolean;
        if (this.needSplitTable) {
            this.connection = paramWeaverConnection;
            this.sql = paramString;
        } else {
            this.sql = paramString;
            this.preparedStatement = paramWeaverConnection.getConnection().prepareStatement(paramString);
        }
    }

    public WeaverPreparedStatement(Connection paramConnection, String paramString, int paramInt1, int paramInt2) throws SQLException {
        paramString = intercepting(paramString);
        WeaverConnection weaverConnection = (WeaverConnection)paramConnection;
        this.preparedStatement = weaverConnection.getConnection().prepareStatement(paramString, paramInt1, paramInt2);
    }

    public WeaverPreparedStatement(Connection paramConnection, String paramString, int[] paramArrayOfint) throws SQLException {
        paramString = intercepting(paramString);
        WeaverConnection weaverConnection = (WeaverConnection)paramConnection;
        this.preparedStatement = weaverConnection.getConnection().prepareStatement(paramString, paramArrayOfint);
    }

    public WeaverPreparedStatement(Connection paramConnection, String paramString, int paramInt) throws SQLException {
        paramString = intercepting(paramString);
        WeaverConnection weaverConnection = (WeaverConnection)paramConnection;
        this.preparedStatement = weaverConnection.getConnection().prepareStatement(paramString, paramInt);
    }

    public WeaverPreparedStatement(Connection paramConnection, String paramString, String[] paramArrayOfString) throws SQLException {
        paramString = intercepting(paramString);
        WeaverConnection weaverConnection = (WeaverConnection)paramConnection;
        this.preparedStatement = weaverConnection.getConnection().prepareStatement(paramString, paramArrayOfString);
    }

    public WeaverPreparedStatement(Connection paramConnection, String paramString, int paramInt1, int paramInt2, int paramInt3) throws SQLException {
        paramString = intercepting(paramString);
        WeaverConnection weaverConnection = (WeaverConnection)paramConnection;
        this.preparedStatement = weaverConnection.getConnection().prepareStatement(paramString, paramInt1, paramInt2, paramInt3);
    }

    public ResultSet executeQuery() throws SQLException {
        if (this.needSplitTable) {
            this.sql = intercepting(this.sql, this.params);
            this.preparedStatement = this.connection.getConnection().prepareStatement(this.sql);
            setParams(this.preparedStatement, this.params);
        }
        beforeExecute(this.sql);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        finishedExecute(this.sql);
        return resultSet;
    }

    public int executeUpdate() throws SQLException {
        if (this.needSplitTable) {
            this.sql = intercepting(this.sql, this.params);
            this.preparedStatement = this.connection.getConnection().prepareStatement(this.sql);
            setParams(this.preparedStatement, this.params);
        }
        beforeExecute(this.sql);
        int i = this.preparedStatement.executeUpdate();
        finishedExecute(this.sql);
        return i;
    }

    public void setNull(int paramInt1, int paramInt2) throws SQLException {
        this.preparedStatement.setNull(paramInt1, paramInt2);
    }

    public void setBoolean(int paramInt, boolean paramBoolean) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), Boolean.valueOf(paramBoolean));
        } else {
            this.preparedStatement.setBoolean(paramInt, paramBoolean);
        }
    }

    public void setByte(int paramInt, byte paramByte) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), Byte.valueOf(paramByte));
        } else {
            this.preparedStatement.setByte(paramInt, paramByte);
        }
    }

    public void setShort(int paramInt, short paramShort) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), Short.valueOf(paramShort));
        } else {
            this.preparedStatement.setShort(paramInt, paramShort);
        }
    }

    public void setInt(int paramInt1, int paramInt2) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt1), Integer.valueOf(paramInt2));
        } else {
            this.preparedStatement.setInt(paramInt1, paramInt2);
        }
    }

    public void setLong(int paramInt, long paramLong) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), Long.valueOf(paramLong));
        } else {
            this.preparedStatement.setLong(paramInt, paramLong);
        }
    }

    public void setFloat(int paramInt, float paramFloat) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), Float.valueOf(paramFloat));
        } else {
            this.preparedStatement.setFloat(paramInt, paramFloat);
        }
    }

    public void setDouble(int paramInt, double paramDouble) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), Double.valueOf(paramDouble));
        } else {
            this.preparedStatement.setDouble(paramInt, paramDouble);
        }
    }

    public void setBigDecimal(int paramInt, BigDecimal paramBigDecimal) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramBigDecimal);
        } else {
            this.preparedStatement.setBigDecimal(paramInt, paramBigDecimal);
        }
    }

    public void setString(int paramInt, String paramString) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramString);
        } else {
            this.preparedStatement.setString(paramInt, paramString);
        }
    }

    public void setBytes(int paramInt, byte[] paramArrayOfbyte) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramArrayOfbyte);
        } else {
            this.preparedStatement.setBytes(paramInt, paramArrayOfbyte);
        }
    }

    public void setDate(int paramInt, Date paramDate) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramDate);
        } else {
            this.preparedStatement.setDate(paramInt, paramDate);
        }
    }

    public void setTime(int paramInt, Time paramTime) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramTime);
        } else {
            this.preparedStatement.setTime(paramInt, paramTime);
        }
    }

    public void setTimestamp(int paramInt, Timestamp paramTimestamp) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramTimestamp);
        } else {
            this.preparedStatement.setTimestamp(paramInt, paramTimestamp);
        }
    }

    public void setAsciiStream(int paramInt1, InputStream paramInputStream, int paramInt2) throws SQLException {
        this.preparedStatement.setAsciiStream(paramInt1, paramInputStream, paramInt2);
    }

    @Deprecated
    public void setUnicodeStream(int paramInt1, InputStream paramInputStream, int paramInt2) throws SQLException {
        this.preparedStatement.setUnicodeStream(paramInt1, paramInputStream, paramInt2);
    }

    public void setBinaryStream(int paramInt1, InputStream paramInputStream, int paramInt2) throws SQLException {
        this.preparedStatement.setBinaryStream(paramInt1, paramInputStream, paramInt2);
    }

    public void clearParameters() throws SQLException {
        this.preparedStatement.clearParameters();
    }

    public void setObject(int paramInt1, Object paramObject, int paramInt2) throws SQLException {
        this.preparedStatement.setObject(paramInt1, paramObject, paramInt2);
    }

    public void setObject(int paramInt, Object paramObject) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramObject);
        } else {
            this.preparedStatement.setObject(paramInt, paramObject);
        }
    }

    public boolean execute() throws SQLException {
        if (this.needSplitTable) {
            this.sql = intercepting(this.sql, this.params);
            this.preparedStatement = this.connection.getConnection().prepareStatement(this.sql);
            setParams(this.preparedStatement, this.params);
        }
        beforeExecute(this.sql);
        boolean bool = this.preparedStatement.execute();
        finishedExecute(this.sql);
        return bool;
    }

    public void addBatch() throws SQLException {
        if (this.needSplitTable) {
            this.batchParams.add(this.params);
            this.params = new HashMap<>();
        } else {
            this.preparedStatement.addBatch();
        }
    }

    public void setCharacterStream(int paramInt1, Reader paramReader, int paramInt2) throws SQLException {
        this.preparedStatement.setCharacterStream(paramInt1, paramReader, paramInt2);
    }

    public void setRef(int paramInt, Ref paramRef) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramRef);
        } else {
            this.preparedStatement.setRef(paramInt, paramRef);
        }
    }

    public void setBlob(int paramInt, Blob paramBlob) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramBlob);
        } else {
            this.preparedStatement.setBlob(paramInt, paramBlob);
        }
    }

    public void setClob(int paramInt, Clob paramClob) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramClob);
        } else {
            this.preparedStatement.setClob(paramInt, paramClob);
        }
    }

    public void setArray(int paramInt, Array paramArray) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramArray);
        } else {
            this.preparedStatement.setArray(paramInt, paramArray);
        }
    }

    public ResultSetMetaData getMetaData() throws SQLException {
        return this.preparedStatement.getMetaData();
    }

    public void setDate(int paramInt, Date paramDate, Calendar paramCalendar) throws SQLException {
        this.preparedStatement.setDate(paramInt, paramDate, paramCalendar);
    }

    public void setTime(int paramInt, Time paramTime, Calendar paramCalendar) throws SQLException {
        this.preparedStatement.setTime(paramInt, paramTime, paramCalendar);
    }

    public void setTimestamp(int paramInt, Timestamp paramTimestamp, Calendar paramCalendar) throws SQLException {
        this.preparedStatement.setTimestamp(paramInt, paramTimestamp, paramCalendar);
    }

    public void setNull(int paramInt1, int paramInt2, String paramString) throws SQLException {
        this.preparedStatement.setNull(paramInt1, paramInt2, paramString);
    }

    public void setURL(int paramInt, URL paramURL) throws SQLException {
        this.preparedStatement.setURL(paramInt, paramURL);
    }

    public ParameterMetaData getParameterMetaData() throws SQLException {
        return this.preparedStatement.getParameterMetaData();
    }

    public void setRowId(int paramInt, RowId paramRowId) throws SQLException {
        this.preparedStatement.setRowId(paramInt, paramRowId);
    }

    public void setNString(int paramInt, String paramString) throws SQLException {
        this.preparedStatement.setNString(paramInt, paramString);
    }

    public void setNCharacterStream(int paramInt, Reader paramReader, long paramLong) throws SQLException {
        this.preparedStatement.setNCharacterStream(paramInt, paramReader, paramLong);
    }

    public void setNClob(int paramInt, NClob paramNClob) throws SQLException {
        this.preparedStatement.setNClob(paramInt, paramNClob);
    }

    public void setClob(int paramInt, Reader paramReader, long paramLong) throws SQLException {
        this.preparedStatement.setClob(paramInt, paramReader, paramLong);
    }

    public void setBlob(int paramInt, InputStream paramInputStream, long paramLong) throws SQLException {
        this.preparedStatement.setBlob(paramInt, paramInputStream, paramLong);
    }

    public void setNClob(int paramInt, Reader paramReader, long paramLong) throws SQLException {
        this.preparedStatement.setNClob(paramInt, paramReader, paramLong);
    }

    public void setSQLXML(int paramInt, SQLXML paramSQLXML) throws SQLException {
        this.preparedStatement.setSQLXML(paramInt, paramSQLXML);
    }

    public void setObject(int paramInt1, Object paramObject, int paramInt2, int paramInt3) throws SQLException {
        this.preparedStatement.setObject(paramInt1, paramObject, paramInt2, paramInt3);
    }

    public void setAsciiStream(int paramInt, InputStream paramInputStream, long paramLong) throws SQLException {
        this.preparedStatement.setAsciiStream(paramInt, paramInputStream, paramLong);
    }

    public void setBinaryStream(int paramInt, InputStream paramInputStream, long paramLong) throws SQLException {
        this.preparedStatement.setBinaryStream(paramInt, paramInputStream, paramLong);
    }

    public void setCharacterStream(int paramInt, Reader paramReader, long paramLong) throws SQLException {
        this.preparedStatement.setCharacterStream(paramInt, paramReader, paramLong);
    }

    public void setAsciiStream(int paramInt, InputStream paramInputStream) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramInputStream);
        } else {
            this.preparedStatement.setAsciiStream(paramInt, paramInputStream);
        }
    }

    public void setBinaryStream(int paramInt, InputStream paramInputStream) throws SQLException {
        if (this.needSplitTable) {
            this.params.put(Integer.valueOf(paramInt), paramInputStream);
        } else {
            this.preparedStatement.setBinaryStream(paramInt, paramInputStream);
        }
    }

    public void setCharacterStream(int paramInt, Reader paramReader) throws SQLException {
        this.preparedStatement.setCharacterStream(paramInt, paramReader);
    }

    public void setNCharacterStream(int paramInt, Reader paramReader) throws SQLException {
        this.preparedStatement.setNCharacterStream(paramInt, paramReader);
    }

    public void setClob(int paramInt, Reader paramReader) throws SQLException {
        this.preparedStatement.setClob(paramInt, paramReader);
    }

    public void setBlob(int paramInt, InputStream paramInputStream) throws SQLException {
        this.preparedStatement.setBlob(paramInt, paramInputStream);
    }

    public void setNClob(int paramInt, Reader paramReader) throws SQLException {
        this.preparedStatement.setNClob(paramInt, paramReader);
    }

    public ResultSet executeQuery(String paramString) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        ResultSet resultSet = this.preparedStatement.executeQuery(paramString);
        finishedExecute(paramString);
        return resultSet;
    }

    public int executeUpdate(String paramString) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        int i = this.preparedStatement.executeUpdate(paramString);
        finishedExecute(paramString);
        return i;
    }

    public void close() throws SQLException {
        this.preparedStatement.close();
    }

    public int getMaxFieldSize() throws SQLException {
        return this.preparedStatement.getMaxFieldSize();
    }

    public void setMaxFieldSize(int paramInt) throws SQLException {
        this.preparedStatement.setMaxFieldSize(paramInt);
    }

    public int getMaxRows() throws SQLException {
        return this.preparedStatement.getMaxRows();
    }

    public void setMaxRows(int paramInt) throws SQLException {
        this.preparedStatement.setMaxRows(paramInt);
    }

    public void setEscapeProcessing(boolean paramBoolean) throws SQLException {
        this.preparedStatement.setEscapeProcessing(paramBoolean);
    }

    public int getQueryTimeout() throws SQLException {
        return this.preparedStatement.getQueryTimeout();
    }

    public void setQueryTimeout(int paramInt) throws SQLException {
        this.preparedStatement.setQueryTimeout(paramInt);
    }

    public void cancel() throws SQLException {
        this.preparedStatement.cancel();
    }

    public SQLWarning getWarnings() throws SQLException {
        return this.preparedStatement.getWarnings();
    }

    public void clearWarnings() throws SQLException {
        this.preparedStatement.clearWarnings();
    }

    public void setCursorName(String paramString) throws SQLException {
        this.preparedStatement.setCursorName(paramString);
    }

    public boolean execute(String paramString) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        boolean bool = this.preparedStatement.execute(paramString);
        finishedExecute(paramString);
        return bool;
    }

    public ResultSet getResultSet() throws SQLException {
        return this.preparedStatement.getResultSet();
    }

    public int getUpdateCount() throws SQLException {
        return this.preparedStatement.getUpdateCount();
    }

    public boolean getMoreResults() throws SQLException {
        return this.preparedStatement.getMoreResults();
    }

    public void setFetchDirection(int paramInt) throws SQLException {
        this.preparedStatement.setFetchDirection(paramInt);
    }

    public int getFetchDirection() throws SQLException {
        return this.preparedStatement.getFetchDirection();
    }

    public void setFetchSize(int paramInt) throws SQLException {
        this.preparedStatement.setFetchSize(paramInt);
    }

    public int getFetchSize() throws SQLException {
        return this.preparedStatement.getFetchSize();
    }

    public int getResultSetConcurrency() throws SQLException {
        return this.preparedStatement.getResultSetConcurrency();
    }

    public int getResultSetType() throws SQLException {
        return this.preparedStatement.getResultSetType();
    }

    public void addBatch(String paramString) throws SQLException {
        this.preparedStatement.addBatch(paramString);
    }

    public void clearBatch() throws SQLException {
        this.preparedStatement.clearBatch();
    }

    public int[] executeBatch() throws SQLException {
        if (this.needSplitTable) {
            this.sql = intercepting(this.sql, this.params);
            this.preparedStatement = this.connection.getConnection().prepareStatement(this.sql);
            for (Map<Integer, Object> map : this.batchParams) {
                setParams(this.preparedStatement, map);
                this.preparedStatement.addBatch();
            }
        }
        return this.preparedStatement.executeBatch();
    }

    public Connection getConnection() throws SQLException {
        return this.preparedStatement.getConnection();
    }

    public boolean getMoreResults(int paramInt) throws SQLException {
        return this.preparedStatement.getMoreResults(paramInt);
    }

    public ResultSet getGeneratedKeys() throws SQLException {
        return this.preparedStatement.getGeneratedKeys();
    }

    public int executeUpdate(String paramString, int paramInt) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        int i = this.preparedStatement.executeUpdate(paramString, paramInt);
        finishedExecute(paramString);
        return i;
    }

    public int executeUpdate(String paramString, int[] paramArrayOfint) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        int i = this.preparedStatement.executeUpdate(paramString, paramArrayOfint);
        finishedExecute(paramString);
        return i;
    }

    public int executeUpdate(String paramString, String[] paramArrayOfString) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        int i = this.preparedStatement.executeUpdate(paramString, paramArrayOfString);
        finishedExecute(paramString);
        return i;
    }

    public boolean execute(String paramString, int paramInt) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        boolean bool = this.preparedStatement.execute(paramString, paramInt);
        finishedExecute(paramString);
        return bool;
    }

    public boolean execute(String paramString, int[] paramArrayOfint) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        boolean bool = this.preparedStatement.execute(paramString, paramArrayOfint);
        finishedExecute(paramString);
        return bool;
    }

    public boolean execute(String paramString, String[] paramArrayOfString) throws SQLException {
        paramString = intercepting(paramString);
        beforeExecute(paramString);
        boolean bool = this.preparedStatement.execute(paramString, paramArrayOfString);
        finishedExecute(paramString);
        return bool;
    }

    public int getResultSetHoldability() throws SQLException {
        return this.preparedStatement.getResultSetHoldability();
    }

    public boolean isClosed() throws SQLException {
        return this.preparedStatement.isClosed();
    }

    public void setPoolable(boolean paramBoolean) throws SQLException {
        this.preparedStatement.setPoolable(paramBoolean);
    }

    public boolean isPoolable() throws SQLException {
        return this.preparedStatement.isPoolable();
    }

    public void closeOnCompletion() throws SQLException {
        this.preparedStatement.closeOnCompletion();
    }

    public boolean isCloseOnCompletion() throws SQLException {
        return this.preparedStatement.isCloseOnCompletion();
    }

    public <T> T unwrap(Class<T> paramClass) throws SQLException {
        return this.preparedStatement.unwrap(paramClass);
    }

    public boolean isWrapperFor(Class<?> paramClass) throws SQLException {
        return this.preparedStatement.isWrapperFor(paramClass);
    }

    private boolean needIntercept(String paramString) {
        return StatementInterceptor.needIntercept(paramString);
    }

    private String intercepting(String paramString) {
        return StatementInterceptor.intercepting(paramString);
    }

    private String intercepting(String paramString, Map<Integer, Object> paramMap) {
        return StatementInterceptor.intercepting(paramString, paramMap);
    }

    private void beforeExecute(String paramString) {}

    private void finishedExecute(String paramString) {}

    public void setParams(PreparedStatement paramPreparedStatement, Map<Integer, Object> paramMap) throws SQLException {
        for (Map.Entry<Integer, Object> entry : paramMap.entrySet()) {
            Integer integer = (Integer)entry.getKey();
            Object object = entry.getValue();
            if (object instanceof Integer || object instanceof Long) {
                paramPreparedStatement.setInt(integer.intValue(), Util.getIntValue("" + object));
                continue;
            }
            if (object == null) {
                paramPreparedStatement.setObject(integer.intValue(), (Object)null);
                continue;
            }
            if (object instanceof Float) {
                paramPreparedStatement.setFloat(integer.intValue(), Util.getFloatValue("" + object));
                continue;
            }
            if (object instanceof BigDecimal || object instanceof Double) {
                paramPreparedStatement.setObject(integer.intValue(), object);
                continue;
            }
            if (object instanceof Date) {
                paramPreparedStatement.setDate(integer.intValue(), (Date)object);
                continue;
            }
            if (object instanceof String || object instanceof Character) {
                if (("" + object).trim().equalsIgnoreCase("")) {
                    paramPreparedStatement.setObject(integer.intValue(), (Object)null);
                    continue;
                }
                if ("null".equals(("" + object).trim().toLowerCase())) {
                    paramPreparedStatement.setObject(integer.intValue(), (Object)null);
                    continue;
                }
                paramPreparedStatement.setString(integer.intValue(), "" + object);
                continue;
            }
            if (object instanceof Clob) {
                paramPreparedStatement.setClob(integer.intValue(), (Clob)object);
                continue;
            }
            if (object instanceof Blob) {
                paramPreparedStatement.setBlob(integer.intValue(), (Blob)object);
                continue;
            }
            paramPreparedStatement.setObject(integer.intValue(), object);
        }
    }
}

